﻿namespace EFMLEditor.Internal
{
    using System.Collections.Generic;
    using System.IO;
    using System.Xml;

    public class ProjectFile
    {
        private XmlDocument doc;

        public Dictionary<string, string> Windows = new Dictionary<string, string>();
        public List<string> Strings = new List<string>();
        public List<string> Resources = new List<string>();

        public string PropsPath;


        public ProjectFile()
        {
            doc = new XmlDocument();
        }

        public void Load(string filename)
        {
            doc.Load(filename);

            var props = doc.GetElementsByTagName("properties")[0];
            var strings = doc.GetElementsByTagName("strings")[0];
            var resources = doc.GetElementsByTagName("resources")[0];
            var windows = doc.GetElementsByTagName("windows")[0];

            PropsPath = props.Attributes["path"].Value;

            foreach (XmlNode w in windows.ChildNodes)
            {
                Windows.Add(w.Attributes["path"].Value, w.Attributes["code"].Value);
            }
            foreach (XmlNode s in strings.ChildNodes)
            {
                Strings.Add(s.Attributes["path"].Value);
            }
            foreach (XmlNode resource in resources.ChildNodes)
            {
                Resources.Add(resource.Attributes["path"].Value);
            }
        }

        public void Save(string filename)
        {
            var root = doc.CreateElement("project");
            var props = doc.CreateElement("properties");

            props.SetAttribute("path", PropsPath);

            var strings = doc.CreateElement("strings");
            var resources = doc.CreateElement("resources");
            var windows = doc.CreateElement("windows");

            foreach (var window in Windows)
            {
                var defaultWin = doc.CreateElement("source");
                defaultWin.SetAttribute("path", window.Key);
                defaultWin.SetAttribute("code", window.Value);

                windows.AppendChild(defaultWin);
            }

            foreach (string s in strings)
            {
                var defaultString = doc.CreateElement("source");
                defaultString.SetAttribute("path", s);
                strings.AppendChild(defaultString);
            }

            foreach (var s in Resources)
            {
                var defaultString = doc.CreateElement("source");
                defaultString.SetAttribute("path", s);
                resources.AppendChild(defaultString);
            }

            props.AppendChild(strings);
            props.AppendChild(resources);

            root.AppendChild(windows);
            root.AppendChild(props);

            doc.AppendChild(root);

            doc.Save(new FileStream(filename, FileMode.OpenOrCreate));
        }

    }

    public class PropsWriter
    {
        public static void Save(string filename, Dictionary<string, string> values)
        {
            var doc = new XmlDocument();
            var root = doc.CreateElement("props");

            foreach (var v in values)
            {
                var el = doc.CreateElement(v.Key);
                el.InnerText = v.Value;
                root.AppendChild(el);
            }

            doc.AppendChild(root);
            var fs = new FileStream(filename, FileMode.OpenOrCreate);
            doc.Save(fs);
            fs.Close();
        }
    }

    public class PropsReader
    {
        public Dictionary<string, string> Values = new Dictionary<string, string>();

        public string Path;

        public string this[string k]
        {
            get
            {
                if(Values.ContainsKey(k))
                    return Values[k];
                return "";
            }
        }

        public void Load(string filename)
        {
            var doc = new XmlDocument();
            doc.Load(filename);

            var fold = new FileInfo(filename);
            Path = fold.DirectoryName;

            var root = doc.GetElementsByTagName("props")[0];
            foreach (XmlNode child in root.ChildNodes)
            {
                var name = child.Name;
                var val = child.InnerText;

                Values.Add(name, val);
            }
        }
    }
}